(function(window, undefined){

//jquery缓动动画扩展
jQuery.easing['jswing'] = jQuery.easing['swing'];
jQuery.extend( jQuery.easing,
{
    def: 'easeOutQuad',
    easeOutQuad: function (x, t, b, c, d) {
        return -c *(t/=d)*(t-2) + b;
    }
});


var pCommon = Egame.add("common");

//tab切换
pCommon.tabs = {
    //对外提供重置全局参数
    opts: {},
    init:function(selector, options){
        var tabs = function(sel, opt){
            var opts = $.extend({}, this.defaults, opt);
            var jcon = $(sel),
                jtabs = jcon.find(opts.tabBox).find(opts.tabItem),
                jcons = jcon.find(opts.conBox).find(opts.conItem);
            jtabs.bind(opts.ev, function(){
                var i = $(this).index();
                jtabs.removeClass(opts.tabOnClass).eq(i).addClass(opts.tabOnClass);
                jcons.hide().eq(i).show();
            });
        };

        tabs.prototype.defaults = {
                //tab标签包裹元素
                tabBox: ".fn_tab",
                //tab标签元素
                tabItem: ".fn_tab_item",
                //内容包裹元素
                conBox: ".fn_con",
                //内容元素
                conItem: ".fn_con_item",
                //tab选中样式名
                tabOnClass: "on",
                //触发事件
                ev: "click"
            };
        tabs.prototype.defaults = $.extend({}, tabs.prototype.defaults, pCommon.tabs.opts);
        return new tabs(selector, options);
    }

};

//图片轮播
pCommon.slider = {
    //对外提供重置全局参数
    opts: {},
    init: function(selector, options){
        var slider = function(sel, opt){
            var me = this;

            //参数
            var opts = $.extend({}, me.defaults, opt);
            var jcon = $(sel),
                //图片索引状态按钮
                jbtns = jcon.find(opts.btnBox).find(opts.btnItem),
                //图片显示框
                jconBox = jcon.find(opts.conBox),
                //图片滚动框
                jconInner = jcon.find(opts.conInner),
                //图片列表
                jcons = jcon.find(opts.conBox).find(opts.conItem),
                //上一页
                jpreBtn = jcon.find(opts.btnPre),
                //下一页
                jnextBtn = jcon.find(opts.btnNext),
                //len:图片个数
                //nlen:克隆首尾图片后个数，用于循环展示
                //curI当前索引值
                //unitW、unitH显示框宽高
                len = jcons.length, nlen = len + 2, curI = 0, unitW = jconBox.width(), unitH = jconBox.height();
            var go2con = null, timer = null, timer2;

            //初始化dom操作
            if (opts.type == "h" || opts.type == "v") {
                jconInner.append(jcons.eq(0).clone());
                jconInner.prepend(jcons.eq(len-1).clone());
                jcons = jcon.find(opts.conBox).find(opts.conItem);
            }
            jcons.css({width: unitW + "px", height: unitH + "px", overflow: "hidden"});


            //向后循环返回初始位置
            function reStart(){
                if (opts.type == "h") {
                    jconInner.css({left: 0});
                }else if (opts.type == "v") {
                    jconInner.css({top: 0});
                }
            }
            //向前循环返回末尾位置
            function reEnd(){
                if (opts.type == "h") {
                    jconInner.css({left: -(len+1)*unitW});
                }else if (opts.type == "v") {
                    jconInner.css({top: -(len+1)*unitH});
                }
            };
            if (opts.type == "h") {
                jconInner.css({left:-unitW + "px", width: nlen * unitW + "px", height: unitH + "px"});
                go2con = function(index){
                    var l = - (index * unitW) - unitW;
                    jbtns.removeClass(opts.tabOnClass).eq(index).addClass(opts.tabOnClass);
                    jconInner.stop().animate({left: l + "px"}, 500, 'easeOutQuad');
                    curI = index;
                };
            }else if (opts.type == "v") {
                jconInner.css({top:-unitH + "px"});
                go2con = function(index){
                    var l = - (index * unitH) - unitH;
                    jbtns.removeClass(opts.tabOnClass).eq(index).addClass(opts.tabOnClass);
                    jconInner.stop().animate({top: l + "px"}, 500, 'easeOutQuad');
                    curI = index;
                };
            }else if(opts.type == "a"){
                jcons.css({position: "absolute"}).hide().eq(curI).show();
                go2con = function(index){
                    if (curI == index) {
                        return;
                    }
                    jbtns.removeClass(opts.tabOnClass).eq(index).addClass(opts.tabOnClass);
                    jcons.stop().hide().eq(index).fadeIn(300);
                    jcons.eq(curI).show().fadeOut(300);
                    curI = index;
                };
            }else if(opts.type == "n"){
                go2con = function(index){
                    var l = - (index * unitH);
                    jbtns.removeClass(opts.tabOnClass).eq(index).addClass(opts.tabOnClass);
                    jconInner.css({top: l + "px"});
                    curI = index;
                };
            }
            jbtns.bind(opts.ev, function(){
                var i = $(this).index();
                go2con(i);
            });
            jpreBtn.click(function(){
                var i = curI - 1;
                if (i < 0) {
                    i = len - 1;
                    reEnd();
                }
                go2con(i);
            });
            jnextBtn.click(function(){
                var i = curI + 1;
                if (i >= len) {
                    i = 0;
                    reStart();
                }
                go2con(i);
            });

            if (opts.auto) {
                jcon.bind("mouseover", function(){
                    clearInterval(timer);
                    timer = null;
                }).bind("mouseout", function(){
                    clearInterval(timer);
                    timer = null;
                    timer = setInterval(function(){
                        var i = curI + 1;
                        if (i >= len) {
                            i = 0;
                        }
                        go2con(i);
                    }, opts.time);
                }).trigger("mouseout");
            }
        };

        slider.prototype.defaults = {
            //按钮包裹元素
            btnBox: ".fn_btnbox",
            //按钮元素
            btnItem: ".fn_btn_item",
            //上一页
            btnPre: ".fn_pre",
            //下一页
            btnNext: ".fn_next",
            //内容包裹元素
            conBox: ".fn_con",
            //内容包裹滚动元素
            conInner: ".fn_con_inner",
            //内容元素
            conItem: ".fn_con_item",
            //tab选中样式名
            tabOnClass: "on",
            //切换类型v(纵向)\h(横向)\a(透明度)\n(无效果)
            type: "h",
            //触发事件
            ev: "mouseover",
            auto: true,
            time: 4000
        };
        slider.prototype.defaults = $.extend({}, slider.prototype.defaults, pCommon.slider.opts);
        return new slider(selector, options);
    }
};

//返回顶部
pCommon.back2top = {
    init: function(selector, options) {
        var defaults = {
            mainW: 960,
            posX: 10,
            posY: 60
        };

        var opts = $.extend({}, defaults, options);
        var jbtn = $(selector);
        var scrollT = 0, timer = null, clientH, clientW,
            btnY = 0,
            btnX = 0,
            lesW = 0;
            btnW = jbtn.width();
        function resetPos() {
            clientH = document.documentElement.clientHeight;
            clientW = document.documentElement.clientWidth;
            lesW = clientW - opts.mainW;
            if (lesW > btnW + opts.posX) {
                btnX = opts.mainW + (lesW/2) + opts.posX;
                jbtn.css({left: btnX, right: ""});
            }else{
                btnX = opts.posX;
                jbtn.css({right: btnX, left: ""});
            }
            btnY = scrollT + clientH - opts.posY;
            jbtn.css({top: btnY  + "px"});

        }
        if (jbtn.length>0) {
            jbtn.css({position: "absolute"});
            resetPos();

            $(window).bind("scroll", function () {
                scrollT = document.documentElement.scrollTop || document.body.scrollTop;
                if (scrollT < 200) {
                    jbtn.hide();
                }else{
                    clearTimeout(timer);
                    timer = null;
                    timer = setTimeout(function() {
                        btnY = scrollT + clientH - opts.posY;
                        jbtn.css({top: btnY  + "px"});
                        jbtn.show();
                    }, 300);
                }
            });

            $(window).bind("resize",function(){
                resetPos();
            });
        }
    }

};

//按钮锁定倒计时
pCommon.timeBtn = {
    //缓存对象
    store:{},
    init:function(id, options){
        var obj = null;
        var lockBtn = function(_id, opt){
            var me = this;
            var defaults = {
                //按钮上锁样式
                lockClass: "locked",
                //倒计时时间
                time: 60,
                //锁定后的文字
                replaceTxt: null
            };

            var opts = $.extend({}, defaults, opt);
            var jbtn = $("#" + _id);
            var jbtnClone = jbtn.clone().attr("id","");
            var jtxtCon = jbtnClone.find(".fn_txt");
            var timer = null;
            //对象方法，用于强制解锁按钮
            this.unlock = function(){
                if (jbtn.data("isLockInit")) {
                    jbtn.show().data("isLock", false);
                    jbtnClone.hide();
                    clearInterval(timer);
                    timer = null;
                }
            };
            this.lock = function(){
                var t = opts.time;
                jtxtCon.html(t);
                if (!jbtn.data("isLock")) {
                    jbtn.data("isLock",true);
                    if (!jbtn.data("isLockInit")) {
                        jbtn.data("isLockInit", true);
                        jbtn.hide().after(jbtnClone);
                        jbtnClone.addClass(opts.lockClass);
                    }else{
                        jbtn.hide();
                        jbtnClone.show();
                    }
                    if (!opts.replaceTxt) {
                        timer = setInterval(function(){
                            if (t<0) {
                                me.unlock();
                            }
                            jtxtCon.html(t--);
                        }, 1000);
                    }else{
                        jbtnClone.html(opts.replaceTxt);
                    }
                }
            };
        };
        //初始化返回新对象并存入缓存，下次初始化同一按钮则返回缓存对象
        if (!pCommon.timeBtn.store[id]) {
            obj = new lockBtn(id, options);
            pCommon.timeBtn.store[id] = obj;
            return obj;
        }else{
            return pCommon.timeBtn.store[id];
        }
    }
};


window.pCommon = pCommon;

})(window);
